package cn.wawi.controller.business;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.wawi.common.interceptor.SellerTokenInterceptor;
import cn.wawi.controller.BaseController;
import cn.wawi.model.business.Member;
import cn.wawi.utils.DateUtil;

import com.jfinal.aop.Before;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.render.JsonRender;
import com.mchange.v2.lang.StringUtils;

@ControllerBind(controllerKey="/memberx")
public class MemberxController extends BaseController<Member>{

	
	
	
	//我的会员
	@Before(SellerTokenInterceptor.class)
	public void findMyMember(){
		StringBuilder sql = new StringBuilder("select * from member A where A.deleted!=1 and A.userId= ? ");		
		String managerId = getRequest().getParameter("managerId"); //员工Id
		String beginDateTime = getRequest().getParameter("beginDateTime");
		String endDatetime = getRequest().getParameter("endDateTime");
		List lt = new ArrayList();
		lt.add(getRequest().getParameter("managerId"));
		
		if(beginDateTime!=null )
		{
		    String start =	DateUtil.formatDateTime(new Date(Long.valueOf(beginDateTime)));
			sql.append(" and A.createTime > ?" );
            lt.add(start);
		}
		
		if(endDatetime!=null){
			   String end =	DateUtil.formatDateTime(new Date(Long.valueOf(endDatetime)));
				sql.append(" and A.createTime < ? " );
               lt.add(end);
		}		
		
		if(StringUtils.nonEmptyString(getRequest().getParameter("checkStatus"))){
			sql.append(" and A.checkStatus = ? ");
			lt.add(getRequest().getParameter("checkStatus"));
		}

		Page<Member> list=getModel(Member.class).paginateMysql(toPageIndex(), toPageSize(),sql.toString(),lt.toArray());
		
	    List<Record> records =	Db.find("select count(*) as totals, checkStatus  from ("+sql.toString()+")X  group by  checkStatus ",lt.toArray());;
		Map<String, Object> map = new HashMap<String, Object>();
    	map.put("unCheckedNum", 0);
    	map.put("checkedNum", 0);
    	map.put("rejectNum", 0);
        for(Record record:records){
        	if(0==record.getInt("checkStatus"))
        	{
            	map.put("unCheckedNum", record.getLong("totals").intValue());

        	}
        	
        	if(1==record.getInt("checkStatus"))
        	{
            	map.put("checkedNum", record.getLong("totals").intValue());

        	}
        	
        	if(2==record.getInt("checkStatus"))
        	{
            	map.put("rejectNum", record.getLong("totals").intValue());

        	} 	
        }
	    json.setRow(map);
		json.setPageNumber(list.getPageNumber());
		json.setRows(list.getList());
		json.setTotal(list.getTotalRow()+0L);
		//审核  位审核     拒绝      时间段
		//充值多少钱  多少点   多少次
        render(new JsonRender(json).forIE());
	}
	
	

	

	

}
